package com.gdones.wms.bus.inbound.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.gdones.common.core.dto.MyPageBand;
import com.gdones.common.core.dto.MyPager;
import com.gdones.common.core.dto.MyResult;
import com.gdones.common.core.util.MyPinYinUtil;
import com.gdones.wms.bus.inbound.dto.InboundDto;
import com.gdones.wms.bus.inbound.dto.WmsInbound;
import com.gdones.wms.bus.inbound.dto.WmsInboundInfo;
import com.gdones.wms.bus.inbound.mapper.WmsInboundInfoMapper;
import com.gdones.wms.bus.inbound.mapper.WmsInboundMapper;
import com.gdones.wms.bus.inbound.service.IWmsInboundService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gdones.wms.bus.record.dto.WmsRecord;
import com.gdones.wms.bus.record.dto.WmsRecordLife;
import com.gdones.wms.bus.record.mapper.WmsRecordLifeMapper;
import com.gdones.wms.bus.record.mapper.WmsRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 仓储入库记录主表 服务实现类
 * </p>
 *
 * @author GaoJingBo
 * @since 2023-05-08
 */
@Service
public class WmsInboundServiceImpl extends ServiceImpl<WmsInboundMapper, WmsInbound> implements IWmsInboundService {

    @Autowired
    private WmsRecordMapper recordMapper;
    
    @Autowired
    private WmsRecordLifeMapper recordLifeMapper;
    
    @Autowired
    private WmsInboundInfoMapper inboundInfoMapper;
    
    @Override
    public MyResult pages(WmsInbound dto, MyPageBand pageBand) {

        MyResult result = new MyResult();

        WmsInboundMapper inboundMapper = getBaseMapper();

        IPage<WmsInbound> pages = inboundMapper.pages(PageDTO.of(pageBand.getPage(), pageBand.getSize()), dto);

        // 根据分页的结果封装返回数据
        MyPager<WmsInbound> pager = new MyPager<>();
        pager.setTotal(pages.getTotal());
        pager.setPage(pageBand.getPage());
        pager.setPageSize(pageBand.getSize());
        pager.setRows(pages.getRecords());

        result.setData(pager);
        result.setMsg("查询成功！");

        return result;
    }

    @Override
    public MyResult mylist(WmsInbound dto) {
        
        MyResult result = new MyResult();

        WmsInboundMapper inboundMapper = getBaseMapper();

        List<WmsInbound> list = inboundMapper.myList(dto);

        result.setMsg("查询成功！");
        result.setData(list);

        return result;
    }

    @Override
    public MyResult add(InboundDto dto) {

        MyResult result = new MyResult();

        WmsInboundMapper wmsInboundMapper = getBaseMapper();

        // 入库总量
        int inCount = 0;
        // 入库明细
        List<WmsInboundInfo> infoList = new ArrayList<>();

        // 入库单
        WmsInbound inbound = dto.getInbound();
        inbound.setInboundID(IdUtil.fastSimpleUUID());
        inbound.setCode("WMIN-M-"+ MyPinYinUtil.getPinYinByString(inbound.getWarehouseName())+"-"+new Date().getTime());
        inbound.setInboundTime(new Date());
        inbound.setRecordTypeCount(dto.getRecordList().size());

        // 入库明细
        List<WmsRecord> recordList = dto.getRecordList();

        for(WmsRecord record : recordList){
            // 每个物资生成编号
            record.setRecordID(IdUtil.fastSimpleUUID());
            record.setCode("WLR-"+ MyPinYinUtil.getPinYinByString(record.getName())+"-"+new Date().getTime());
            recordMapper.insert(record);

            // 入库明准备
            WmsInboundInfo info = new WmsInboundInfo();
            info.setRecordID(record.getRecordID());
            info.setRecordName(record.getName());
            info.setInCount(record.getWmsCount());
            info.setInboundID(inbound.getInboundID());
            infoList.add(info);
            // 入库明细保存
            inboundInfoMapper.insert(info);

            // 准备物资入库记录
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            WmsRecordLife life = new WmsRecordLife();
            life.setRecordID(record.getRecordID());
            life.setRecordName(record.getName());
            life.setLifeType("物资入库");
            life.setLifeTime(new Date());
            life.setLifeDesc(record.getName()+"("+record.getCode()+")，入库数量：【"+record.getWmsCount()+"】，入库到："+record.getWarehouseName()+"，入库时间："+(sdf.format(inbound.getInboundTime()))+"，入库人："+inbound.getUserName());
            recordLifeMapper.insert(life);

            inCount += record.getWmsCount();
        }

        // 入库单
        inbound.setInCount(inCount);
        wmsInboundMapper.insert(inbound);


        result.setMsg("入库成功！");

        return result;
    }

    @Override
    public MyResult deletes(String ids) {

        MyResult result = new MyResult();

        String[] split = ids.split(",");

        List<String> list = Arrays.asList(split);

        boolean b = this.removeBatchByIds(list);

        if(b){
            result.setMsg("删除成功！");
        }else{
            result.setMsg("删除失败！");
            result.setCode("9999");
        }

        return result;
    }

    @Override
    public MyResult getByID(String id) {

        MyResult result = new MyResult();

        InboundDto dto = new InboundDto();

        WmsInboundMapper wmsInboundMapper = getBaseMapper();

        // 入库单
        WmsInbound wmsInbound = wmsInboundMapper.selectById(id);

        // 入库明细
        QueryWrapper<WmsInboundInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("inboundID",id);

        List<WmsInboundInfo> infoList = inboundInfoMapper.selectList(queryWrapper);

        dto.setInbound(wmsInbound);
        dto.setInfoList(infoList);

        result.setMsg("查询成功！");
        result.setData(dto);

        return result;
    }
}
